﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using System.Data.SqlClient;

using CustomsSystem.Model;
using Maticsoft.DBUtility;

namespace CustomsSystem.DAL
{
    public class CustomerService
    {
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from Cu_Customer");
            strSql.Append(" where ID=" + ID + " ");
            return DbHelperSQL.Exists(strSql.ToString());
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public int Add(CustomsSystem.Model.Customer model)
        {
            StringBuilder strSql = new StringBuilder();
            StringBuilder strSql1 = new StringBuilder();
            StringBuilder strSql2 = new StringBuilder();
            if(model.TaxNo!=null)
            {
                strSql1.Append(" TaxNo,");
                strSql2.Append("'"+model.TaxNo+"',");
            }
            if(model.InvType!=null)
            {
            strSql1.Append("InvType,");
            strSql2.Append("'"+model.InvType+"',");
            }
            if (model.CustomerName != null)
            {
                strSql1.Append("CustomerName,");
                strSql2.Append("'" + model.CustomerName + "',");
            }
            if (model.Invoice != null)
            {
                strSql1.Append("Invoice,");
                strSql2.Append("'" + model.Invoice + "',");
            }
            if (model.Address != null)
            {
                strSql1.Append("Address,");
                strSql2.Append("'" + model.Address + "',");
            }
            if (model.PostCode != null)
            {
                strSql1.Append("PostCode,");
                strSql2.Append("'" + model.PostCode + "',");
            }
            if (model.CustomerPhone != null)
            {
                strSql1.Append("CustomerPhone,");
                strSql2.Append("'" + model.CustomerPhone + "',");
            }
            if (model.Fax != null)
            {
                strSql1.Append("Fax,");
                strSql2.Append("'" + model.Fax + "',");
            }
            if (model.CustomerType != null)
            {
                strSql1.Append("CustomerType,");
                strSql2.Append("'" + model.CustomerType + "',");
            }
            if (model.Remarks != null)
            {
                strSql1.Append("Remarks,");
                strSql2.Append("'" + model.Remarks + "',");
            }
            if (model.CustomerNO != null)
            {
                strSql1.Append("CustomerNO,");
                strSql2.Append("'" + model.CustomerNO + "',");
            }
            if (model.Abbreviation != null)
            {
                strSql1.Append("Abbreviation,");
                strSql2.Append("'" + model.Abbreviation + "',");
            }
            if (model.Email != null)
            {
                strSql1.Append("Email,");
                strSql2.Append("'" + model.Email + "',");
            }
            strSql.Append("insert into Cu_Customer(");
            strSql.Append(strSql1.ToString().Remove(strSql1.Length - 1));
            strSql.Append(")");
            strSql.Append(" values (");
            strSql.Append(strSql2.ToString().Remove(strSql2.Length - 1));
            strSql.Append(")");
            strSql.Append(";select @@IDENTITY");
            object id = DbHelperSQL.GetSingle(strSql.ToString());
            if (id == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(id);
            }
        }

        /// <summary>
        /// 更新一条数据
        /// </summary>
        public bool Update(CustomsSystem.Model.Customer model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update Cu_Customer set ");
            if(model.TaxNo!=null)
            {
                strSql.Append("TaxNo ='"+model.TaxNo+"',");
            }
            else
            {
                strSql.Append("TaxNo=null,");
            }
            if(model.InvType!=null)
            {
                strSql.Append("InvType ='"+model.InvType+"',");
            }
            else
            {
                strSql.Append("InvType=null,");
            }
            if (model.CustomerName != null)
            {
                strSql.Append("CustomerName='" + model.CustomerName + "',");
            }
            else
            {
                strSql.Append("CustomerName= null ,");
            }
            if (model.Invoice != null)
            {
                strSql.Append("Invoice='" + model.Invoice + "',");
            }
            else
            {
                strSql.Append("Invoice= null ,");
            }
            if (model.Address != null)
            {
                strSql.Append("Address='" + model.Address + "',");
            }
            else
            {
                strSql.Append("Address= null ,");
            }
            if (model.PostCode != null)
            {
                strSql.Append("PostCode='" + model.PostCode + "',");
            }
            else
            {
                strSql.Append("PostCode= null ,");
            }
            if (model.CustomerPhone != null)
            {
                strSql.Append("CustomerPhone='" + model.CustomerPhone + "',");
            }
            else
            {
                strSql.Append("CustomerPhone= null ,");
            }
            if (model.Fax != null)
            {
                strSql.Append("Fax='" + model.Fax + "',");
            }
            else
            {
                strSql.Append("Fax= null ,");
            }
            if (model.CustomerType != null)
            {
                strSql.Append("CustomerType='" + model.CustomerType + "',");
            }
            else
            {
                strSql.Append("CustomerType= null ,");
            }
            if (model.Remarks != null)
            {
                strSql.Append("Remarks='" + model.Remarks + "',");
            }
            else
            {
                strSql.Append("Remarks= null ,");
            }

            if (model.CustomerNO != null)
            {
                strSql.Append("CustomerNO='" + model.CustomerNO + "',");
            }
            else
            {
                strSql.Append("CustomerNO= null ,");
            } if (model.Abbreviation != null)
            {
                strSql.Append("Abbreviation='" + model.Abbreviation + "',");
            }
            else
            {
                strSql.Append("Abbreviation= null ,");
            } if (model.Email != null)
            {
                strSql.Append("Email='" + model.Email + "',");
            }
            else
            {
                strSql.Append("Email= null ,");
            }
            int n = strSql.ToString().LastIndexOf(",");
            strSql.Remove(n, 1);
            strSql.Append(" where ID=" + model.ID + " ");
            int rowsAffected = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条数据
        /// </summary>
        public bool Delete(int ID)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append("delete from Cu_Customer ");
            strSql.Append(" where ID=" + ID + " ");
            int rowsAffected = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rowsAffected > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CustomsSystem.Model.Customer GetModel(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1  ");
            strSql.Append(" ID,CustomerNO,TaxNO,InvType,Abbreviation,CustomerName,Invoice,Address,PostCode,CustomerPhone,Fax,CustomerType,Email,Remarks ");
            strSql.Append(" from Cu_Customer ");
            strSql.Append(" where ID=" + ID + " ");
            CustomsSystem.Model.Customer model = new CustomsSystem.Model.Customer();
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CustomsSystem.Model.Customer GetModel(string strwhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1  ");
            strSql.Append("  ID,CustomerNO,TaxNO,InvType,Abbreviation,CustomerName,Invoice,Address,PostCode,CustomerPhone,Fax,CustomerType,Email,Remarks ");
            strSql.Append(" from Cu_Customer ");
            strSql.Append(" where CustomerName='" +strwhere+"'");
            CustomsSystem.Model.Customer model = new CustomsSystem.Model.Customer();
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            if (ds.Tables[0].Rows.Count > 0)
            {
                return DataRowToModel(ds.Tables[0].Rows[0]);
            }
            else
            {
                return null;
            }
        }

        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public CustomsSystem.Model.Customer DataRowToModel(DataRow row)
        {
            CustomsSystem.Model.Customer model = new CustomsSystem.Model.Customer();
            if (row != null)
            {
                if (row["ID"] != null && row["ID"].ToString() != "")
                {
                    model.ID = int.Parse(row["ID"].ToString());
                }
                if (row["TaxNo"] != null && row["TaxNo"].ToString() != "")
                {
                    model.TaxNo = row["TaxNo"].ToString();
                }
                if (row["InvType"] != null && row["InvType"].ToString() != "")
                {
                    model.InvType = int.Parse(row["InvType"].ToString());
                }
                if (row["CustomerName"] != null)
                {
                    model.CustomerName = row["CustomerName"].ToString();
                }
                if (row["Invoice"] != null)
                {
                    model.Invoice = row["Invoice"].ToString();
                }
                if (row["Address"] != null)
                {
                    model.Address = row["Address"].ToString();
                }
                if (row["PostCode"] != null)
                {
                    model.PostCode = row["PostCode"].ToString();
                }
                if (row["CustomerPhone"] != null)
                {
                    model.CustomerPhone = row["CustomerPhone"].ToString();
                }
                if (row["Fax"] != null)
                {
                    model.Fax = row["Fax"].ToString();
                }
                if (row["CustomerType"] != null)
                {
                    model.CustomerType = row["CustomerType"].ToString();
                }
                if (row["Remarks"] != null)
                {
                    model.Remarks = row["Remarks"].ToString();
                }

                if (row["CustomerNO"] != null)
                {
                    model.CustomerNO = row["CustomerNO"].ToString();
                }
                if (row["Abbreviation"] != null)
                {
                    model.Abbreviation = row["Abbreviation"].ToString();
                }
                if (row["Email"] != null)
                {
                    model.Email = row["Email"].ToString();
                }
            }
            return model;
        }

        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetViewList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select a.ID as cID,a.CustomerNO,a.Abbreviation,a.CustomerName,a.Invoice,a.Address,a.PostCode,a.TaxNO,InvType=case a.InvType when 1 then '普通发票' when 2 then '专用发票' end,b.codeName,a.Remarks,c.ID,c.Customer_ID,c.CPName,CPSex=case c.CPSex when 0 then '男' when 1 then '女' end,c.CPPost,c.MobilePhone,c.CPPhone,C.Fax,C.Email,C.Remarks as Rem  ");
            strSql.Append("  FROM Cu_Customer a left join Info_code b on(b.codeType='clientType' and b.codeID=a.CustomerType) left join Cu_Contact c on(a.ID=c.Customer_ID)  ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by codename");
            return DbHelperSQL.Query(strSql.ToString());
        }
        /// <summary>
        /// 获得数据列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select a.ID,a.CustomerNO,a.Abbreviation,a.CustomerName,a.Invoice,a.TaxNO,a.InvType,a.Address,a.PostCode,a.CustomerPhone,a.Fax,b.codeName,a.CustomerType,a.Email,a.Remarks ");
            strSql.Append(" FROM Cu_Customer a left join Info_code b on(b.codeType='clientType' and b.codeID=a.CustomerType) ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获取记录总数
        /// </summary>
        public int GetRecordCount(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) FROM Cu_Customer ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            object obj = DbHelperSQL.GetSingle(strSql.ToString());
            if (obj == null)
            {
                return 0;
            }
            else
            {
                return Convert.ToInt32(obj);
            }
        }
        //===========================================================================

        /*public static DataSet GetCustomerDataSet()
        {
            DataSet ds=new DataSet();
            try
            {
                DBHelper.conn.Open();
                string strCmd = string.Format("select * from Cu_Customer");
                ds= DBHelper.ExecuteDataSet(strCmd);
            }
            catch (Exception ex) { }
            finally
            {
                DBHelper.conn.Close();
            }
            return ds;
        }

        public static DataSet GetCustomerDataSet(string id)
        {
            DataSet ds=new DataSet();
            try
            {
                DBHelper.conn.Open();
                string strCmd = string.Format("select * from Cu_Customer where ID='{0}'", id);
                //if (DBHelper.SqlFilter(strCmd)) return null;//条件过滤
                ds = DBHelper.ExecuteDataSet(strCmd);
            }
            catch(Exception ex){} 
            finally
            { 
                DBHelper.conn.Close();
            }
            return ds;
        }
        /// <summary>
        /// 插入一个客户信息
        /// </summary>
        /// <param name="cus"></param>
        /// <returns></returns>
        public static bool InsertCustomer(Customer cus)
        {
            try
            {
                DBHelper.conn.Open();
                string strCmd = string.Format
                    ("INSERT INTO Cu_Customer(CustomerName,Invoice,Address,PostCode,CustomerPhone,Fax,CustomerType,Remarks) VALUES('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}')",
                    cus.CustomerName,cus.Invoice,cus.Address,cus.PostCode,cus.CustomerPhone,cus.Fax,cus.CustomerType,cus.Remarks);
                int n=DBHelper.ExecuteNonQuery(strCmd);
                if (n == -1) return false;
                else return true;
            }
            catch  { }
            finally { DBHelper.conn.Close(); }
            return false;
        }

        /// <summary>
        /// 更新一个客户信息
        /// </summary>
        /// <param name="cus"></param>
        /// <returns></returns>
        public static bool UpdateCustomer(Customer cus)
        {
            try
            {
                DBHelper.conn.Open();
                string strCmd = string.Format(
                    "UPDATE Cu_Customer SET CustomerName='{0}',Invoice='{1}',Address='{2}',PostCode='{3}',CustomerPhone='{4}',FAX='{5}',CustomerType='{6}',Remarks='{7}' WHERE ID='{8}'",
                    cus.CustomerName, cus.Invoice, cus.Address, cus.PostCode, cus.CustomerPhone, cus.Fax, cus.CustomerType, cus.Remarks,cus.ID);
                int n = DBHelper.ExecuteNonQuery(strCmd);
                if (n == -1) return false;
                else return true;
            }
            catch { }
            finally { DBHelper.conn.Close(); }
            return false;
        }
        /// <summary>
        /// 删除一个客户信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static bool DelCustomer(string id)
        {
            try
            {
                DBHelper.conn.Open();
                string strCmd = string.Format(
                    "DELETE FROM Cu_Customer WHERE ID='{0}'",id);
                int n = DBHelper.ExecuteNonQuery(strCmd);
                if (n == -1) return false;
                else return true;
            }
            catch { }
            finally { DBHelper.conn.Close(); }
            return false;
        }
        */
    }
}
